import { SDKDataMod } from "../SDKDataMode";
import { SDKTools } from "../SDKTools";
import { ADMgr } from "../广告播放类/ADMgr";
import { WebDataProxy } from "./WebDataProxy";

export class AdData {
    private _data: SDKDataMod.AdWebData = null;

    public SetData(data: SDKDataMod.AdWebData) {
        this._data = data;
    }

    public get m_AdWebData(): SDKDataMod.AdWebData {
        return this._data;
    }

    public PlayAdList: string[] = [];

    public get m_IOS_Force_Click_Ok(): boolean {
        let now = SDKTools.NowTime;
        let time = Number(this._data.force_click_time) + Number(this._data.video_play_time)
        console.log(`m_IOS_Force_Click_Ok  ${now} -- ${ADMgr.start_time} -- ${time} -- ${now - ADMgr.start_time > time}`)
        return now - ADMgr.start_time > time
    }

    public get m_Android_Force_Click_Ok(): boolean {
        console.log(`m_IOS_Force_Click_Ok -- ${ADMgr.start_time} -- ${ADMgr.end_time} -- ${this._data.force_click_time}`)
        if (ADMgr.start_time <= 0 || ADMgr.end_time <= 0) {
            return false;
        }
        if (ADMgr.end_time - ADMgr.start_time < Number(this._data.force_click_time)) {
            return false;
        }
        return true;
    }
    force_click: boolean = false;
    public GetPlayAd(): {
        force_click: boolean,
        ad_unit: string,
    } {
        if (this._data.reward_adunits.length == this.PlayAdList.length) {
            this.PlayAdList = [];
        }
        let notAry = this._data.reward_adunits.filter((item) => {
            return this.PlayAdList.indexOf(item.ad_unit) == -1;
        });
        let index = Math.floor(Math.random() * notAry.length);
        let x = Math.random();
        // 剩余未播放的广告位大于等于强制点击次数 - 1
        let count = WebDataProxy.inst.m_TaskFinishCount
        if (count >= this._data.force_click_count - 1) {
            // 随机数小于等于强制点击概率
            if (x <= Number(this._data.force_click_percent)) {
                this.force_click = true;
            }
        }
        console.log("GetPlayAd", this.force_click,notAry[index].ad_unit);
        return {
            force_click:this.force_click,
            ad_unit: notAry[index].ad_unit
        }
    }

    public UpdatePlayAdCount(ad_unit: string) {
        this.force_click = false;
        if (this.PlayAdList.indexOf(ad_unit) != -1) {
            return
        }
        this.PlayAdList.push(ad_unit);
    }
}